package com.bs.visitservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bs.visitservice.cache.CacheManager;
import com.bs.visitservice.cache.ICache;
import com.bs.visitservice.entity.StandardCode;
import com.bs.visitservice.entity.StandardCodeType;
import com.bs.visitservice.service.StandardCodeService;
import com.bs.visitservice.service.StandardCodeTypeService;
import com.bs.visitservice.utils.ParamUtils;
import com.bs.visitservice.utils.Result;
import com.bs.visitservice.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典管理
 */
@RestController
@RequestMapping("/standardCode")
public class StandardCodeController {

    @Autowired
    private StandardCodeService standardCodeService;
    @Autowired
    private StandardCodeTypeService standardCodeTypeService;

    /**
     * 添加数据字典类型
     * @param cond
     * @return
     */
    @RequestMapping(value = "/saveCodeType",method = RequestMethod.POST)
    public Result saveCodeType(@RequestBody Map cond){
        StandardCodeType standardCodeType = (StandardCodeType) ResultUtil.transObject(cond,StandardCodeType.class);
        boolean isSuc = standardCodeTypeService.save(standardCodeType);
        List<Map<String,Object>> data = new ArrayList<>();
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("codeTypeNo", standardCodeType.getNo());
        data.add(dataMap);
        if(isSuc){
            return Result.success(data);
        }
        return Result.error(null);
    }

    /**
     * 添加数据字典
     * @param cond
     * @return
     */
    @RequestMapping(value = "/saveCode",method = RequestMethod.POST)
    public Result saveCode(@RequestBody List<Map> cond){
        List<StandardCode> standardCodeList = new ArrayList<>();
        for(int i = 0;i<cond.size();i++){
            StandardCode standardCode = (StandardCode) ResultUtil.transObject(cond.get(i),StandardCode.class);
            if(standardCode.getSn() != null){
                standardCode.setSn(standardCode.getSn());
            }
            standardCodeList.add(standardCode);
        }
        boolean isSuc = standardCodeService.saveBatch(standardCodeList);
        if(isSuc){
            return Result.success(null);
        }
        return Result.error(null);
    }

    /**
     * 根据数据字典类型查找数据字典数据
     * @param cond
     * @return
     */
    @RequestMapping(value = "/queryCodeByType",method = RequestMethod.POST)
    public Result queryCodeByType(@RequestBody Map cond){
        Integer typeNo = (Integer)cond.get("typeNo");
        LambdaQueryWrapper<StandardCode> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StandardCode::getTypeNo,typeNo).orderByAsc(StandardCode::getSn);
        List<Map<String, Object>> maps = standardCodeService.listMaps(wrapper);
        return Result.success(maps);
    }

    @RequestMapping(value = "/queryCodeType",method = RequestMethod.POST)
    public Result queryCodeType(@RequestBody Map cond){
        IPage page = ParamUtils.getPage(cond);
        page = standardCodeTypeService.pageMaps(page);
        List<Map<String,Object>> records = page.getRecords();
        return Result.success(records);
    }

    @RequestMapping(value = "/updateStandardCodeCache",method = RequestMethod.GET)
    public Result init() {
        CacheManager.instance().initCache("standardCode","local");
        ICache standardCodeCache = CacheManager.instance().getCacheProvider("standardCode");
        List<StandardCode> standardCodeList = standardCodeService.lambdaQuery().orderByAsc(StandardCode::getTypeName).orderByAsc(StandardCode::getSn).list();
        Map<String,Object> allData = new HashMap<>();
        standardCodeList.forEach(s->{
            String typeName = s.getTypeName();
            if(!allData.containsKey(typeName)){
                allData.put(typeName, new ArrayList<StandardCode>());
            }
            List<StandardCode> list = (ArrayList<StandardCode>)allData.get(typeName);
            list.add(s);
        });
        standardCodeCache.putAll(allData);
        return Result.success(null);
    }
}
